﻿#include <iostream>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <map>
#include "mkcallback.h"
#include "cameraconfig.h"
#include "storagemanager.h"
#include "redismanager.h"

using namespace std;

//全局变量
mk_sem gSem;
map<string, mk_proxy_player> gRtspProxys;

int main(int argc ,char* argv[])
{
    //mk库配置初始化
    char *ini_path = mk_util_get_exe_dir("config.ini");
    mk_config mkConfig;
    mkConfig.ini = ini_path;
    mkConfig.ini_is_path = 1;
    mkConfig.log_level = LOG_LEV;
    mkConfig.log_mask = LOG_FILE | LOG_CONSOLE;
    mkConfig.log_file_path = "./log";
    mkConfig.log_file_days = 7;
    mkConfig.ssl = NULL;
    mkConfig.ssl_pwd = NULL;
    mkConfig.thread_num = 8;

    //初始化mk库运行环境
    mk_env_init(&mkConfig);
    mk_free(ini_path);
    //初始化rtsp服务
    mk_rtsp_server_start(554, 0);

    //配置系统事件回调
    mk_events events;
    events.on_mk_media_changed = on_mk_media_changed;
    events.on_mk_media_publish = NULL;
    events.on_mk_media_play = NULL;
    events.on_mk_media_not_found = NULL;
    events.on_mk_media_no_reader = NULL;
    events.on_mk_http_request = NULL;
    events.on_mk_http_access = NULL;
    events.on_mk_http_before_access = NULL;
    events.on_mk_rtsp_get_realm = NULL;
    events.on_mk_rtsp_auth = NULL;
    events.on_mk_record_mp4 = on_mk_record_mp4;
    events.on_mk_shell_login = NULL;
    events.on_mk_flow_report = NULL;
    mk_events_listen(&events);

    log_printf(LOG_LEV, ">>>-------------------------------<<<");
    log_printf(LOG_LEV, "流媒体服务V1.3 By ZXT 20250415");
    log_printf(LOG_LEV, "注：rtsp组播功能指定udp_multicast方式拉流");

    //1 初始化redis配置
    log_printf(LOG_LEV, "初始化redis");
    STU_REDIS_CONFIG config;
    config.redis_ip = mk_get_option("custom.redis_ip");
    config.redis_port =  stoi(mk_get_option("custom.redis_port"));
    config.redis_pwd = mk_get_option("custom.redis_passwd");
    config.default_db = stoi(mk_get_option("custom.redis_db"));
    STU_REDIS_CHANNEL channel;
    channel.cmdAddFile = mk_get_option("custom.cmdAddFile");
    channel.cmdDelFile = mk_get_option("custom.cmdDelFile");
    channel.keyAddFile = mk_get_option("custom.keyAddFile");
    channel.keyDelFile = mk_get_option("custom.keyDelFile");
    channel.mediaServiceCmdChannel = mk_get_option("custom.mediaServiceCmdChannel");
    channel.cmdDatetimeUpdate = mk_get_option("custom.cmdDatetimeUpdate");
    channel.cmdStorage = mk_get_option("custom.cmdStorage");
    channel.keyDatetimeUpdate = mk_get_option("custom.keyDatetimeUpdate");
    channel.keyStorage = mk_get_option("custom.keyStorage");
    channel.controllerCmdChannel = mk_get_option("custom.controllerCmdChannel");
    RedisManager::getInstance()->initRedis(config, channel);
    RedisManager::getInstance()->startTask();

    //2 初始化文件管理配置
    log_printf(LOG_LEV, "初始化文件管理");
    string str1 = mk_get_option("custom.auto_delete_file_count");
    string str2 = mk_get_option("custom.auto_delete_file_size");
    string str3 = mk_get_option("custom.auto_delete_mod");
    string str4 = mk_get_option("custom.max_cache");
    string str5 = mk_get_option("custom.max_days");
    string str6 = mk_get_option("protocol.mp4_save_path");
    StorageManager::getInstacne()->initStorageParam(str6, stoi(str4), stoi(str5), stoi(str3), stoi(str1), stoi(str2));

    //3 初始化相机配置
    avdevice_register_all();
    av_log_set_level(AV_LOG_WARNING);
    log_printf(LOG_LEV, "初始化相机配置");
    CameraConfig cameraConfig;
    char *json_path = mk_util_get_exe_dir("cameras.json");
    cameraConfig.initConfig(json_path);
    cameraConfig.startProxyPull();

    //设置退出信号
    gSem = mk_sem_create();
    signal(SIGINT, [](int)
    {
        log_printf(LOG_LEV, "SIGINT:exit");
        mk_sem_post(gSem, 1);
    });
    signal(SIGTERM, [](int)
    {
        log_printf(LOG_LEV, "SIGTERM:exit");
        mk_sem_post(gSem, 1);
    });
    mk_sem_wait(gSem);
    mk_sem_release(gSem);

    log_printf(LOG_LEV, "开始释放资源 即将退出程序");
    cameraConfig.stopProxyPull();
    mk_stop_all_server();
    RedisManager::getInstance()->stopTask();
    log_printf(LOG_LEV, "释放资源结束 退出程序");

    return 0;
}
